window.onload = function AdminSetDivSize()
{
	var e = document.getElementById('mc_admin_msg_cont');
	e.style.width = (document.body.offsetWidth - 700) + "px";
}

function AdminNewMessage()
{
	var txt = prompt("Texte du message", "");
	if(txt == null) return;
	AdminAddMessage("<b>" + user + "</b> : " + txt);
}

function AdminNewTodo()
{
	var txt = prompt("Texte du message", "");
	if(txt == null) return;
	AdminAddMessage("<b>Todo</b> : " + txt);
}

function AdminAddMessage(t)
{
	GetPageContent("GET", jMC_ROOT + "admin/messages.php?&do=new&txt=" + encodeURIComponent(t), "", CheckNew, t, true);
}

function CheckNew(Success, Text, Param)
{
	var e = document.getElementById("mc_admin_msg");
	var html = '<div class="mc_admin_msg_item ' + Param.charAt(3) + '" name="' + Text + '"><span class="mc_admin_msg_del"><a href="#" onclick="AdminDelMessage(this);return false;">X</a></span>' + Param + '</div>';
	if(e.insertAdjacentHTML)
	{	
		e.insertAdjacentHTML("afterBegin", html);
	}
	else
	{
		var range = document.createRange();
	    var frag = range.createContextualFragment(html);
		e.insertBefore(frag, e.firstChild);
	}	
}

function AdminDelMessage(e)
{
	var div = e.parentNode.parentNode;
	var Id = div.getAttribute("name");
	GetPageContent("GET", jMC_ROOT + "admin/messages.php?&do=del&id=" + Id, "", CheckDel, null, true); 
	div.parentNode.removeChild(div);
}

function CheckDel(Success, Text, Element)
{

}

function GetHTTPObject()
{
	var xhr; 
    try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xhr = new XMLHttpRequest();     }
          catch (e3) {  xhr = false;   }
        }
    }
	return xhr;
}

function GetPageContent(Type, Url, Params, Callback, cbParam, Sync)
{
	var xhr = GetHTTPObject();
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && Callback != null)
		{
			if(xhr.status == 200 || xhr.status == 0)
				Callback(true, xhr.responseText, cbParam);
			else
				CallBack(false, "Error code : " + xhr.status, cbParam);
		}
	};

	xhr.open(Type, Url, Sync);
	if(Type == "POST")
	{
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(Params);
	}
	else
		xhr.send(null);
}